﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using QuickModel.DuctFastModel;

namespace QuickModel.Command
{
	[Transaction(TransactionMode.Manual)]
	public class CmdDuctFastModel : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			commandData.Application.Application.FailuresProcessing += this.OnFailuresProcessing;
			QuickModelFrameWork quickModelFrameWork = new QuickModelFrameWork(new DuctRequestMaker(commandData)
			{
				MaxDuctWidth = 1800.0
			}, new DuctRespondHanlder());
			quickModelFrameWork.Excute(commandData);
			commandData.Application.Application.FailuresProcessing -= this.OnFailuresProcessing;
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
		{
			FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
			IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
			bool flag = failureMessages.Count == 0;
			if (flag)
			{
				bool flag2 = e.GetProcessingResult() > 0;
				if (!flag2)
				{
					e.SetProcessingResult((FailureProcessingResult)0);
				}
			}
			else
			{
				foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
				{
					bool flag3 = failureMessageAccessor.GetSeverity() == 2;
					if (flag3)
					{
						failuresAccessor.ResolveFailure(failureMessageAccessor);
					}
					else
					{
						bool flag4 = failureMessageAccessor.GetSeverity() == 1;
						if (flag4)
						{
							failuresAccessor.DeleteWarning(failureMessageAccessor);
						}
					}
				}
				e.SetProcessingResult((FailureProcessingResult)1);
			}
		}
	}
}
